package nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;

/**
 * @ClassName NioDiscardClient
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/8/27 14:41
 * @Version 1.0
 */
public class NioDiscardClient {
    public static void send(String message) throws IOException {
        var buffer = ByteBuffer.allocate(1024);
        var socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 1989));
        socketChannel.configureBlocking(false);

        while (!socketChannel.finishConnect()) {
            System.out.println("尝试连接");
        }
        System.out.println("连接成功");

        buffer.put(message.getBytes());
        buffer.flip();
        socketChannel.write(buffer);
        socketChannel.shutdownOutput();
        socketChannel.close();
    }

    public static void main(String[] args) throws IOException {
        send("再见");
    }
}
